package com.docmanage.web.controller.am;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.docmanage.am.domain.dto.DirectoryRoleSaveDTO;
import com.docmanage.am.domain.dto.RoleDirectorySaveDTO;
import com.docmanage.am.domain.vo.FileDirectoryTreeVo;
import com.docmanage.am.service.impl.AmFileDirectoryServiceImpl;
import com.docmanage.am.service.impl.AmRoleDirectoryServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.docmanage.common.annotation.Log;
import com.docmanage.common.core.controller.BaseController;
import com.docmanage.common.core.domain.AjaxResult;
import com.docmanage.common.enums.BusinessType;
import com.docmanage.am.domain.AmFileDirectory;
import com.docmanage.common.utils.poi.ExcelUtil;
import com.docmanage.common.core.page.TableDataInfo;

/**
 * 目录管理Controller
 * 
 * @author docmanage
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/am/directory")
public class AmFileDirectoryController extends BaseController
{
    @Resource
    private AmFileDirectoryServiceImpl amFileDirectoryService;

    @Resource
    private AmRoleDirectoryServiceImpl amRoleDirectoryService;

    /**
     * 查询目录管理列表
     */
    @PreAuthorize("@ss.hasPermi('am:directory:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmFileDirectory amFileDirectory)
    {
        startPage();
        List<AmFileDirectory> list = amFileDirectoryService.selectAmFileDirectoryList(amFileDirectory);
        return getDataTable(list);
    }



    /**
     * 根据权限查询目录管理树结构
     */
    @GetMapping("/role_tree")
    public AjaxResult roleTree()
    {
        return success(amFileDirectoryService.selectAmFileDirectoryTree(0L));
    }

    /**
     * 根据权限查询目录管理树结构
     */
    @GetMapping("/tree")
    public AjaxResult tree(String directory)
    {
        return success(amFileDirectoryService.AmFileDirectoryTree(0L,directory));
    }

    /**
     * 导出目录管理列表
     */
    @PreAuthorize("@ss.hasPermi('am:directory:export')")
    @Log(title = "目录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmFileDirectory amFileDirectory)
    {
        List<AmFileDirectory> list = amFileDirectoryService.selectAmFileDirectoryList(amFileDirectory);
        ExcelUtil<AmFileDirectory> util = new ExcelUtil<AmFileDirectory>(AmFileDirectory.class);
        util.exportExcel(response, list, "目录管理数据");
    }

    /**
     * 获取目录管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('am:directory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(amFileDirectoryService.selectAmFileDirectoryById(id));
    }

    /**
     * 新增目录管理
     */
    @PreAuthorize("@ss.hasPermi('am:directory:add')")
    @Log(title = "目录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AmFileDirectory amFileDirectory)
    {
        return toAjax(amFileDirectoryService.insertAmFileDirectory(amFileDirectory));
    }

    /**
     * 修改目录管理
     */
    @PreAuthorize("@ss.hasPermi('am:directory:edit')")
    @Log(title = "目录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmFileDirectory amFileDirectory)
    {
        return toAjax(amFileDirectoryService.updateAmFileDirectory(amFileDirectory));
    }

    /**
     * 删除目录管理
     */
    @PreAuthorize("@ss.hasPermi('am:directory:remove')")
    @Log(title = "目录管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(amFileDirectoryService.deleteAmFileDirectoryByIds(ids));
    }

//----------------------------------目录角色关联信息------------------------------
    /**
     * 新增目录角色关联关系
     */
    @PreAuthorize("@ss.hasPermi('am:role_directory:add')")
    @Log(title = "新增目录角色关联关系", businessType = BusinessType.INSERT)
    @PostMapping("/role_directory")
    public AjaxResult addRoleDirectory(@RequestBody RoleDirectorySaveDTO directorySaveDTO)
    {
        return toAjax(amRoleDirectoryService.insertAmRoleDirectory(directorySaveDTO));
    }
    /**
     * 新增目录角色关联关系
     */
    @PreAuthorize("@ss.hasPermi('am:role_directory:add')")
    @Log(title = "新增目录角色关联关系", businessType = BusinessType.INSERT)
    @PostMapping("/directory_role")
    public AjaxResult addDirectoryRole(@RequestBody DirectoryRoleSaveDTO directorySaveDTO)
    {
        return toAjax(amRoleDirectoryService.insertAmDirectoryRole(directorySaveDTO));
    }

    /**
     * 获取与角色id绑定的目录id
     */
    @GetMapping(value = "/role_directory/{roleId}")
    public AjaxResult getDirectoryId(@PathVariable("roleId") Long roleId)
    {
        return success(amRoleDirectoryService.getDirectoryId(roleId));
    }

    /**
     * 获取与角色id绑定的目录id
     */
    @GetMapping(value = "/directory_role/{directoryId}")
    public AjaxResult getRoleIds(@PathVariable("directoryId") Long directoryId)
    {
        return success(amRoleDirectoryService.getRoleIds(directoryId));
    }

}
